package ooday03;
/* 引用数组类型的演示 */
public class RefArrayDemo {
    public static void main(String[] args) {
        /*
        //声明Dog型数组dogs，包含3个元素，每个元素都是Dog型，默认值为null
        Dog[] dags = new Dog[3];
        //声明Chick型数组chicks，包含3个元素，每个元素都是Chick型，默认值为null
        Chick[] chicks = new Chick[3];
        //声明Fish型数组fish，包含2个元素，每个元素都是Fish型，默认值为null
        Fish[] fish = new Fish[2];
         */
        //演示引用类型数组与基本类型数组的第一点区别
        Dog[]dogs = new Dog[3];
        dogs[0] = new Dog("小狗1",1,"黑色");
        dogs[1] = new Dog("小狗2",2,"白色");
        dogs[2] = new Dog("小狗3",3,"黄色");

        Chick[]chicks = new Chick[2];
        chicks[0] = new Chick("小鸡1",1,"黑色");
        chicks[1] = new Chick("小鸡2",2,"白色");

        Fish[]fish = new Fish[4];
        fish[0] = new Fish("小花",2,"赤色");
        fish[1] = new Fish("小花2",3,"橙色");
        fish[2] = new Fish("小花3",4,"黄色");
        fish[3] = new Fish("小花4",5,"绿色");


        //演示引用类型数组与基本类型数组的第二点区别
        Dog[] dogss = new Dog[3];
        dogss[0] = new Dog("小狗1",1,"黑色");
        dogss[1] = new Dog("小狗2",2,"白色");
        dogss[2] = new Dog("小狗3",3,"黄色");
        //输出第一只狗的名字
        System.out.println(dogss[0].name);
        //修改第二只狗的年龄为4岁
        dogss[1].age = 4;
        //输出第二只狗修改后的年龄
        System.out.println(dogss[1].age);
        for(int i=0;i<dogs.length;i++){ //遍历dogs数组
            System.out.println(dogs[i].name); //输出每只狗狗的名字
            dogs[i].eat(); //每只狗狗吃饭
        }

        Chick[] chicks1 = new Chick[2];
        chicks[0] = new Chick("小花",1,"花");
        chicks[1] = new Chick("大花",2,"花");
        for(int i=0;i<chicks.length;i++){ //遍历chicks数组
            System.out.println(chicks[i].name);//输出每只鸡的名字
        }

        Fish[] fish1 = new Fish[4];
        fish[0] = new Fish("小金",2,"金");
        fish[1] = new Fish("大金",4,"金");
        fish[2] = new Fish("小绿",1,"绿");
        fish[3] = new Fish("小红",3,"红");
        for(int i=0;i<fish.length;i++){//遍历fish数组
            System.out.println(fish[i].color);//输出每只鱼的颜色
        }
    }
}
